package com.example.calendar.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.example.calendar.ui.screens.category.CategoryDetailScreen
import com.example.calendar.ui.screens.category.CategoryListScreen
import com.example.calendar.ui.screens.category.EditCategoryScreen
import com.example.calendar.ui.screens.day.DayScreen
import com.example.calendar.ui.screens.event.CreateEventScreen
import com.example.calendar.ui.screens.event.EditEventScreen
import com.example.calendar.ui.screens.event.EventDetailScreen
import com.example.calendar.ui.screens.month.MonthScreen
import com.example.calendar.ui.screens.settings.SettingsScreen
import com.example.calendar.ui.screens.tasks.EditTaskScreen
import com.example.calendar.ui.screens.tasks.TaskDetailScreen
import com.example.calendar.ui.screens.tasks.TasksScreen
import com.example.calendar.ui.screens.week.WeekScreen

/**
 * Screen route definitions for navigation
 */
sealed class Screen(val route: String) {
    // Main navigation
    object Month : Screen("month")
    object Week : Screen("week")
    object Day : Screen("day")
    object Tasks : Screen("tasks")
    object Settings : Screen("settings")
    
    // Event related screens
    object EventDetail : Screen("event/detail/{eventId}") {
        fun createRoute(eventId: String) = "event/detail/$eventId"
    }
    object EventCreate : Screen("event/create")
    object EventEdit : Screen("event/edit/{eventId}") {
        fun createRoute(eventId: String) = "event/edit/$eventId"
    }
    
    // Task related screens
    object TaskDetail : Screen("task/detail/{taskId}") {
        fun createRoute(taskId: String) = "task/detail/$taskId"
    }
    object TaskCreate : Screen("task/create")
    object TaskEdit : Screen("task/edit/{taskId}") {
        fun createRoute(taskId: String) = "task/edit/$taskId"
    }
    
    // Category related screens
    object Categories : Screen("categories")
    object CategoryDetail : Screen("category/detail/{categoryId}") {
        fun createRoute(categoryId: String) = "category/detail/$categoryId"
    }
    object CategoryCreate : Screen("category/create")
    object CategoryEdit : Screen("category/edit/{categoryId}") {
        fun createRoute(categoryId: String) = "category/edit/$categoryId"
    }
}

/**
 * Main navigation component for the calendar app
 */
@Composable
fun CalendarNavHost(
    navController: NavHostController,
    modifier: Modifier = Modifier
) {
    NavHost(
        navController = navController,
        startDestination = Screen.Month.route,
        modifier = modifier
    ) {
        // Main navigation
        composable(Screen.Month.route) {
            MonthScreen(navController = navController)
        }
        
        composable(Screen.Week.route) {
            WeekScreen(navController = navController)
        }
        
        composable(Screen.Day.route) {
            DayScreen(navController = navController)
        }
        
        composable(Screen.Tasks.route) {
            TasksScreen(navController = navController)
        }
        
        composable(Screen.Settings.route) {
            SettingsScreen(navController = navController)
        }
        
        // Event routes
        composable(
            route = Screen.EventDetail.route,
            arguments = listOf(
                navArgument("eventId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val eventId = backStackEntry.arguments?.getString("eventId") ?: ""
            EventDetailScreen(eventId = eventId, navController = navController)
        }
        
        composable(Screen.EventCreate.route) {
            CreateEventScreen(navController = navController)
        }
        
        composable(
            route = Screen.EventEdit.route,
            arguments = listOf(
                navArgument("eventId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val eventId = backStackEntry.arguments?.getString("eventId") ?: ""
            EditEventScreen(eventId = eventId, navController = navController)
        }
        
        // Task routes
        composable(
            route = Screen.TaskDetail.route,
            arguments = listOf(
                navArgument("taskId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val taskId = backStackEntry.arguments?.getString("taskId") ?: ""
            TaskDetailScreen(taskId = taskId, navController = navController)
        }
        
        composable(Screen.TaskCreate.route) {
            EditTaskScreen(taskId = null, navController = navController)
        }
        
        composable(
            route = Screen.TaskEdit.route,
            arguments = listOf(
                navArgument("taskId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val taskId = backStackEntry.arguments?.getString("taskId") ?: ""
            EditTaskScreen(taskId = taskId, navController = navController)
        }
        
        // Category routes
        composable(Screen.Categories.route) {
            CategoryListScreen(navController = navController)
        }
        
        composable(
            route = Screen.CategoryDetail.route,
            arguments = listOf(
                navArgument("categoryId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val categoryId = backStackEntry.arguments?.getString("categoryId") ?: ""
            CategoryDetailScreen(categoryId = categoryId, navController = navController)
        }
        
        composable(Screen.CategoryCreate.route) {
            EditCategoryScreen(categoryId = null, navController = navController)
        }
        
        composable(
            route = Screen.CategoryEdit.route,
            arguments = listOf(
                navArgument("categoryId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val categoryId = backStackEntry.arguments?.getString("categoryId") ?: ""
            EditCategoryScreen(categoryId = categoryId, navController = navController)
        }
    }
}

/**
 * Placeholder screen for routes that haven't been implemented yet
 */
@Composable
fun PlaceholderScreen(text: String) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text,
            style = MaterialTheme.typography.headlineMedium
        )
    }
} 